<?php

namespace core\lib;

class Config
{
    static public $conf = array();

    static public function get($file, $name)
    {
        /**
         * 1. 判断配置文件是否存在
         * 2. 判断对应配置是否存在
         * 3. 缓存配置
         */
        if (isset(self::$conf[$file])) {
            return self::$conf[$file][$name];
        } else {
            $path = BASE_PATH . '/config/' . $file . '.php';
            if (is_file($path)) {
                $conf = include $path;
                if (isset($conf[$name])) {
                    self::$conf[$file] = $conf;
                    return $conf[$name];
                } else {
                    error($file . 'config file is empty', 404);
                }
            } else {
                error($file . 'config file is not exists', 404);
            }
        }
    }

    static public function all($file)
    {
        if (isset(self::$conf[$file])) {
            return self::$conf[$file];
        } else {
            $path = BASE_PATH . '/config/' . $file . '.php';
            if (is_file($path)) {
                $conf = include $path;
                self::$conf[$file] = $conf;
                return $conf;
            } else {
                throw new Exception([
                    'msg' => $file . 'config file is not exists',
                    'code' => 404
                ]);
            }
        }
    }
}
